package com.fastloan.jn.mgt.application.basic.controller;

import com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus;
import com.fastloan.jn.mgt.application.basic.model.JnApplication;
import com.fastloan.jn.mgt.application.basic.service.JnApplicationService;
import com.fastloan.jn.mgt.process.exception.AppApproveException;
import com.fastloan.jn.mgt.process.service.ProcessService;
import com.fastloan.jn.utils.EnumUtil;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yanwe
 * createTime 2017-10-2017/10/31 14:53
 */
@RestController
@RequestMapping("/api/web/application")
public class JnApplicationController extends BaseController<JnApplication> {

    @Autowired
    private JnApplicationService jnApplicationService;


    @Autowired
    private ProcessService processService;

    @PostMapping("/pageList")
    public ResultTo pageList() {
        Map<String, Object> map = new HashMap<>();
        map.put("customerManagerId", ManagerKit.getUserId());
        return dataPage("selectApplicationList", map);
    }

    @PostMapping
    public ResultTo addApplication(JnApplication jnApplication) {
        jnApplication.setApplicationStatus(ApplicationStatus.CREATE_ING.getType());
        jnApplication.setCustomerManagerId(ManagerKit.getUserId());
        adaptService.insertSelective(jnApplication);
        return new ResultTo().setData(jnApplication.getId());
    }

    @PostMapping("/upload/{applicationId}")
    public ResultTo uploadFile(@PathVariable("applicationId") String applicationId
            , @RequestPart("files") MultipartFile[] files) {
        jnApplicationService.uploadFile(applicationId, files);
        return new ResultTo();
    }

    @PostMapping("/file/pageList")
    public ResultTo filePageList() {
        Map<String, Object> map = new HashMap<>();
        map.put("customerManagerId", ManagerKit.getUserId());
        return dataPage("selectApplicationFileList", map);
    }

    /**
     * 执行审批流程
     *
     * @param applicationId
     * @return
     */
    @PutMapping("/approve/{applicationId}")
    public ResultTo test(@PathVariable String applicationId) {
        try {
            processService.applicationApproval(applicationId);
            return new ResultTo();
        } catch (AppApproveException e) {
            e.printStackTrace();
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }

    }

    @GetMapping("/applicationStatus")
    public ResultTo getApplicationStatus() {
        return new ResultTo().setData(EnumUtil.convertSelectEnum2SelectModel(ApplicationStatus.values()));
    }
}

